package com.bwie.service.Impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.bwie.service.CommonService;
import com.bwie.utils.R;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @BelongsProject: E-Commerce-Month-01
 * @BelongsPackage: com.bwie.service.Impl
 * @Author: asus
 * @CreateTime: 2025-10-24  14:27
 * @Description:
 * @Version: 1.0
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private FastFileStorageClient storageClient;

    @Override
    public R upload(MultipartFile file)  {

        //获取文件的拓展名
        String extName = FileNameUtil.extName(file.getOriginalFilename());

        //uploadFile 上传接口需要三个参数 输入流、大小、拓展名 null
        StorePath storePath = null;
        try {
            storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //获取 url
        String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
        return R.OK(url);
    }
}
